{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "### 数组创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "# shift + enter 表示执行\n",
    "# shift + tab 表示查看方法的属性、使用\n",
    "# tab 自动补全"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4 5 2 9 5]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([4, 5, 2, 9, 5])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = [4,5,2,9,5]\n",
    "\n",
    "b = np.array(a)\n",
    "print(b)\n",
    "display(b) # 显示b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0, 0, 0, 0],\n",
       "        [0, 0, 0, 0]],\n",
       "\n",
       "       [[0, 0, 0, 0],\n",
       "        [0, 0, 0, 0]],\n",
       "\n",
       "       [[0, 0, 0, 0],\n",
       "        [0, 0, 0, 0]],\n",
       "\n",
       "       [[0, 0, 0, 0],\n",
       "        [0, 0, 0, 0]],\n",
       "\n",
       "       [[0, 0, 0, 0],\n",
       "        [0, 0, 0, 0]]], dtype=int8)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1 = np.zeros(shape = (5,2,4),dtype = np.int8)\n",
    "a1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1., 1., 1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1., 1., 1.]],\n",
       "\n",
       "       [[1., 1., 1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1., 1., 1.]],\n",
       "\n",
       "       [[1., 1., 1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1., 1., 1.]]], dtype=float32)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2 = np.ones(shape = (3,4,6),dtype = np.float32)\n",
    "a2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[8888.9999, 8888.9999, 8888.9999],\n",
       "        [8888.9999, 8888.9999, 8888.9999],\n",
       "        [8888.9999, 8888.9999, 8888.9999],\n",
       "        [8888.9999, 8888.9999, 8888.9999]],\n",
       "\n",
       "       [[8888.9999, 8888.9999, 8888.9999],\n",
       "        [8888.9999, 8888.9999, 8888.9999],\n",
       "        [8888.9999, 8888.9999, 8888.9999],\n",
       "        [8888.9999, 8888.9999, 8888.9999]]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成任意指定数值的数组\n",
    "a3 = np.full(shape = (2,4,3),fill_value = 8888.9999)\n",
    "a3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[86, 88, 58, 87, 35],\n",
       "       [63, 52, 73, 39, 49],\n",
       "       [69,  8, 43, 20, 59],\n",
       "       [50, 40, 37, 11, 72]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 在4-89间随机生成4行5列的int类型的数值\n",
    "a4 = np.random.randint(4,89,size = (4,5))\n",
    "a4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0.7615243 , 0.33238666, 0.70773356, 0.87876069, 0.25106584],\n",
       "        [0.86724827, 0.28525231, 0.92301489, 0.55112441, 0.46068626],\n",
       "        [0.5709599 , 0.6738613 , 0.18962964, 0.29131996, 0.30541031]],\n",
       "\n",
       "       [[0.12684719, 0.37985855, 0.59772229, 0.18832068, 0.40578961],\n",
       "        [0.67541784, 0.77139638, 0.01533588, 0.69602599, 0.8701666 ],\n",
       "        [0.69941619, 0.10791047, 0.11886396, 0.73960608, 0.36822824]],\n",
       "\n",
       "       [[0.08690247, 0.50712991, 0.86333612, 0.32530418, 0.81161074],\n",
       "        [0.26845582, 0.89886453, 0.91267156, 0.20604349, 0.98658723],\n",
       "        [0.80762802, 0.32555932, 0.29143967, 0.01456716, 0.32005519]],\n",
       "\n",
       "       [[0.09344941, 0.75199651, 0.46531049, 0.56061143, 0.5957675 ],\n",
       "        [0.52244969, 0.77249886, 0.46846967, 0.98883465, 0.29992148],\n",
       "        [0.01665902, 0.930401  , 0.46255638, 0.48243493, 0.4486529 ]]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 在0-1间随机生成4行3列5排的数值\n",
    "a5 = np.random.rand(4,3,5)\n",
    "a5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 2.47144347,  0.1220684 ],\n",
       "        [-0.78404057, -0.10964449],\n",
       "        [ 0.37631786,  1.05626462],\n",
       "        [ 0.74452589,  0.93954139],\n",
       "        [ 1.32834085, -0.31239952]],\n",
       "\n",
       "       [[-1.37173127, -0.91874079],\n",
       "        [ 0.30328972,  0.26255267],\n",
       "        [-0.45848996,  0.23502469],\n",
       "        [ 0.90164176, -0.62646772],\n",
       "        [ 0.30803032,  1.02499795]],\n",
       "\n",
       "       [[-0.28022472,  0.70926292],\n",
       "        [-1.43461623,  1.59410811],\n",
       "        [-0.75399074,  0.82393389],\n",
       "        [ 1.83919041, -0.29345919],\n",
       "        [-0.19673227, -0.21852096]]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 正态分布，平均值为0，标准差为1\n",
    "a6 = np.random.randn(3,5,2)\n",
    "a6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[175.00502044, 187.93598516, 172.91411659, 185.1185109 ],\n",
       "        [191.29560868, 189.59524084, 192.09376496, 191.23844779]],\n",
       "\n",
       "       [[194.22709624, 182.99427113, 189.72398182, 181.37002483],\n",
       "        [196.15441536, 194.11942889, 180.9434867 , 196.50377765]],\n",
       "\n",
       "       [[187.68190381, 191.80516308, 181.83759154, 193.04614203],\n",
       "        [177.74937397, 189.53648234, 182.38830067, 190.51679446]],\n",
       "\n",
       "       [[195.90745283, 187.69857909, 187.077634  , 183.90081612],\n",
       "        [187.97406234, 187.51843273, 199.05694416, 189.47948617]],\n",
       "\n",
       "       [[188.70168623, 194.01664157, 182.03125519, 182.87100473],\n",
       "        [197.59083574, 193.28714758, 196.68485714, 180.77965948]]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 正态分布，平均值loc，标准差scale，数据组size\n",
    "a7 = np.random.normal(loc = 189,scale = 5,size = (5,2,4))\n",
    "a7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 4,  9, 14, 19, 24, 29, 34, 39, 44, 49, 54, 59, 64, 69, 74, 79])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 等差数列，左闭右开，84取不到,step可不写\n",
    "a8 = np.arange(4,84,step = 5)\n",
    "a8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([23.        , 24.94871795, 26.8974359 , 28.84615385, 30.79487179,\n",
       "       32.74358974, 34.69230769, 36.64102564, 38.58974359, 40.53846154,\n",
       "       42.48717949, 44.43589744, 46.38461538, 48.33333333, 50.28205128,\n",
       "       52.23076923, 54.17948718, 56.12820513, 58.07692308, 60.02564103,\n",
       "       61.97435897, 63.92307692, 65.87179487, 67.82051282, 69.76923077,\n",
       "       71.71794872, 73.66666667, 75.61538462, 77.56410256, 79.51282051,\n",
       "       81.46153846, 83.41025641, 85.35897436, 87.30769231, 89.25641026,\n",
       "       91.20512821, 93.15384615, 95.1025641 , 97.05128205, 99.        ])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 等差数列，左闭右闭，都取得到，num可不写\n",
    "a9 = np.linspace(23,99,num = 40)\n",
    "a9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# 删除数组\n",
    "del a1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "### 查看数组属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[-0.48192163, -0.17882029],\n",
       "        [-0.39747448,  1.03222027],\n",
       "        [ 0.67119084,  1.48730384]],\n",
       "\n",
       "       [[ 0.25184798,  0.48032374],\n",
       "        [-0.02552754, -0.72637312],\n",
       "        [-0.35036706,  1.58505145]],\n",
       "\n",
       "       [[-0.79806383,  0.27539428],\n",
       "        [ 0.48598446,  0.97407249],\n",
       "        [-0.70138244,  0.56712304]],\n",
       "\n",
       "       [[-0.62637726, -0.53663121],\n",
       "        [ 2.2540928 , -1.06927574],\n",
       "        [ 1.2659247 , -0.63773638]]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "b1 = np.random.randn(4,3,2)\n",
    "b1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(8, 5, 8)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看数组形状\n",
    "b1.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看数组类型(位)\n",
    "b1.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "320"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看数组总数，8*5*8\n",
    "b1.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看数组维度\n",
    "b1.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看条目长度(字节)\n",
    "b1.itemsize\n",
    "\n",
    "# 数据类型64位，1个字节8位，64/8=8字节"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "### 文件读写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[62, 87, 83, 77],\n",
       "        [73, 71, 29, 80],\n",
       "        [72, 24, 31, 44]],\n",
       "\n",
       "       [[24, 68, 81, 36],\n",
       "        [85, 25, 44, 63],\n",
       "        [41, 57, 58, 37]]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[-0.76530663, -0.31956927],\n",
       "       [ 0.64841477,  1.45810308],\n",
       "       [-0.56863285,  0.01739921],\n",
       "       [-1.19789102,  2.34822974]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[-0.76530663, -0.31956927],\n",
       "       [ 0.64841477,  1.45810308],\n",
       "       [-0.56863285,  0.01739921],\n",
       "       [-1.19789102,  2.34822974]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c1 = np.random.randint(24,88,size = (2,3,4))\n",
    "c2 = np.random.randn(4,2)\n",
    "display(c1,c2)\n",
    "c1\n",
    "c2 # 交互式输出，有多个变量时，只能输出最后一个变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# 把数据存到一个文件中\n",
    "np.save('./test_data1',c2)\n",
    "\n",
    "# 把多个数据存到一个文件中\n",
    "np.savez('./test_data2',data_c1 = c1,data_c2 = c2)\n",
    "\n",
    "# 文件压缩存储\n",
    "np.savez_compressed('./test_data3.npz',data1_c1 = c1,\n",
    "                    data2_c2 = c2)\n",
    "\n",
    "# 存为txt或csv格式,只能存储一维或二维的数组\n",
    "np.savetxt(fname = './test_data4.txt',\n",
    "          X = c2,\n",
    "          fmt = '%0.2f',\n",
    "          delimiter = ',')\n",
    "\n",
    "np.savetxt(fname = './test_data5.csv',\n",
    "          X = c2,\n",
    "          fmt = '%0.2f',\n",
    "          delimiter = ',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.76530663, -0.31956927],\n",
       "       [ 0.64841477,  1.45810308],\n",
       "       [-0.56863285,  0.01739921],\n",
       "       [-1.19789102,  2.34822974]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取单数据文件\n",
    "np.load('./test_data1.npy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.76530663, -0.31956927],\n",
       "       [ 0.64841477,  1.45810308],\n",
       "       [-0.56863285,  0.01739921],\n",
       "       [-1.19789102,  2.34822974]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取多数据文件,取值时要加单引号--方法一\n",
    "c3 = np.load('./test_data2.npz')\n",
    "c3['data_c2']\n",
    "\n",
    "# 读取多数据文件,取值时要加单引号--方法二\n",
    "np.load('./test_data2.npz')['data_c2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.77, -0.32],\n",
       "       [ 0.65,  1.46],\n",
       "       [-0.57,  0.02],\n",
       "       [-1.2 ,  2.35]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取txt\n",
    "np.loadtxt('./test_data4.txt',delimiter = \",\")\n",
    "np.loadtxt('./test_data5.csv',delimiter = \",\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "### 数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "hidden": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# int8,int16,int32,int64,uint8(无符号)\n",
    "# int8:表示2**8个数字，256个，-128至127\n",
    "# uint8:表示256个数字，无符号，只有正数，0至255之间,不在此区间的会另行标记显示\n",
    "    \n",
    "# float16,float32,float64\n",
    "\n",
    "# str(字符串类型)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 5, 2], dtype=int8)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([3,5,2],dtype = np.int8)\n",
    "\n",
    "# np.array([-23,-34,123,34,277,0,189],dtype = np.uint8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.7744 , 0.982  , 0.9004 , ..., 0.11646, 0.1586 , 0.04184],\n",
       "       [0.707  , 0.6006 , 0.203  , ..., 0.4233 , 0.9946 , 0.566  ],\n",
       "       [0.4082 , 0.3152 , 0.1615 , ..., 0.1431 , 0.5483 , 0.1104 ],\n",
       "       ...,\n",
       "       [0.00327, 0.4634 , 0.9014 , ..., 0.0715 , 0.5664 , 0.5386 ],\n",
       "       [0.721  , 0.0316 , 0.7627 , ..., 0.6626 , 0.2942 , 0.935  ],\n",
       "       [0.1373 , 0.0293 , 0.47   , ..., 0.3086 , 0.9497 , 0.1152 ]],\n",
       "      dtype=float16)"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d1 = np.random.rand(23,99) # 默认为float64,可不写\n",
    "\n",
    "# 更改数据类型\n",
    "np.asarray(d1,dtype = 'float16')  # 方法一\n",
    "d1.astype(dtype = np.float16)  # 方法二"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "### 数组运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([42, 59, 32, 54, 45, 25, 64, 57])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([58, 52, 60, 94, 58, 79, 71, 95])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "e1 = np.random.randint(18,68,size = 8)\n",
    "e2 = np.random.randint(45,98,size = 8)\n",
    "\n",
    "display(e1,e2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "e1 - e2\n",
    "e1 * e2\n",
    "e1 / e2\n",
    "e1**e2\n",
    "np.power(2,3) # 表示2的3次幂"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True,  True,  True,  True,  True,  True,  True])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 逻辑运算\n",
    "e1 > e2\n",
    "e1 < e2\n",
    "e1 == e2 # 逻辑判断，是否相等"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.025     , 0.04      , 0.02631579, 0.03333333, 0.01923077,\n",
       "       0.01886792, 0.04      , 0.02173913])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组与标量计算，0不能作为分母\n",
    "e1 + 10\n",
    "e1 - 20\n",
    "e1 / 100\n",
    "1 / e1 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# -=、+=、*= 可直接改变原数组，不支持/=\n",
    "e1 -= 23\n",
    "e1 += 10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "### 复制和视图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1, 88,  8,  8])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([ 0,  1, 88,  8,  8])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 赋值=的前后变量是相等的，改变被引用赋值的数据，\n",
    "#赋值数据也随之改变\n",
    "f1 = np.random.randint(0,10,size = 5)\n",
    "f2 = f1\n",
    "f1 is f2\n",
    "f1[2] = 88\n",
    "display(f1,f2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 90, 888,  14,  78, 999])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([ 90, 888,  14,  78, 999])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 含视图的变量是不相等的，含视图的变量不拥有自己的数据，\n",
    "# 但其中任一变量的数组改变，两者均会改变\n",
    "f3 = np.random.randint(0,100,size = 5)\n",
    "f4 = f3.view()\n",
    "f3 is f4\n",
    "f3.flags.owndata # 查看是否为f3自己的数据\n",
    "f4.flags.owndata\n",
    "f3[1] = 888\n",
    "f4[4] = 999\n",
    "display(f3,f4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 33, 888,  22,  73,  86])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([ 33,  74,  22,  73, 999])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 复制/深拷贝的变量是不相等且无关系，各自拥有各自的数据，\n",
    "# 修改任一变量的数组，只改变被修改变量自身的数组\n",
    "# copy在不需要原来的数组情况下使用，切片后调用\n",
    "f5 = np.random.randint(0,100,size = 5)\n",
    "f6 = f5.copy()\n",
    "f5 is f6\n",
    "f6.base is f5\n",
    "f3.flags.owndata\n",
    "f4.flags.owndata\n",
    "f5[1] = 888\n",
    "f6[4] = 999\n",
    "display(f5,f6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 索引、切片和迭代"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 3,  7,  6, 12, 11,  5, 27,  3, 28, 21])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 基本索引切片\n",
    "g1 = np.random.randint(0,30,size = 10)\n",
    "g1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 一维数组取值\n",
    "g[5]\n",
    "g[3,5,7] # 取多项值\n",
    "g[n:m] # 从第n+1个数开始到第m个数,n不写则默认为从0开始，\n",
    "       # m不写则默认取值到最后，左闭右开\n",
    "g[n:m:l] # 从第n+1个数开始到第m个数，每l个数取一个\n",
    "g[n:m:-l] # 反向取数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "low >= high",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-32-a42df557fbce>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 多维数组取值\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mg2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0.30\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0msize\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mg2\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;31m# 取第4、2、6行整行的值\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mg2\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;31m# 取第4行第5列的值\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mmtrand.pyx\u001b[0m in \u001b[0;36mnumpy.random.mtrand.RandomState.randint\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;32m_bounded_integers.pyx\u001b[0m in \u001b[0;36mnumpy.random._bounded_integers._rand_int32\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: low >= high"
     ]
    }
   ],
   "source": [
    "# 多维数组取值\n",
    "g2 = np.random.randint(0.30,size = (10,10))\n",
    "\n",
    "g2[[3,1,5]] # 取第4、2、6行整行的值\n",
    "g2[3,4] # 取第4行第5列的值\n",
    "g2[2,[3,4,5]] # 在第3行，取第4、5、6个数\n",
    "g2[2::7,1::3] # 从第2行到尾，每7行中取一行；再从取出的行中，\n",
    "              # 从第2个数开始到尾，每3个数取一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 花式索引（取数组），是复制/深拷贝\n",
    "g3 = np.arange(20)\n",
    "g4 = g3[[3,4,5,6]] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[144, 111, 136],\n",
       "       [ 67,  68,  17],\n",
       "       [ 52, 144, 141],\n",
       "       [106,  46,  36],\n",
       "       [104,   1, 129],\n",
       "       [ 68,   9,  29],\n",
       "       [120, 142, 120],\n",
       "       [ 65,  35, 105],\n",
       "       [ 85,  82,  48],\n",
       "       [ 92,  28,  75],\n",
       "       [138, 126,  53],\n",
       "       [ 35,  29, 104],\n",
       "       [135,  50, 128],\n",
       "       [ 14, 129,   0],\n",
       "       [ 70,  18,  23],\n",
       "       [ 99,  80, 140],\n",
       "       [ 42,  97,  21],\n",
       "       [128,  66,  79],\n",
       "       [ 58,  60,  92],\n",
       "       [ 65,  49, 109],\n",
       "       [149,  55,  25],\n",
       "       [ 18, 148,  43],\n",
       "       [109,   8,  23],\n",
       "       [ 79,  77, 119],\n",
       "       [ 83,  27,  66],\n",
       "       [116, 117, 123],\n",
       "       [ 43, 126, 147],\n",
       "       [ 48,  21, 103],\n",
       "       [130,  43, 105],\n",
       "       [104,  10,  84],\n",
       "       [ 24,  72,  94],\n",
       "       [  4,  58,  28],\n",
       "       [ 99, 133,  84],\n",
       "       [ 86,  49,  37],\n",
       "       [147,   2, 143],\n",
       "       [ 72, 131, 134],\n",
       "       [ 58, 131,  44],\n",
       "       [ 34,  59,  25],\n",
       "       [ 82,   8, 144],\n",
       "       [149, 129, 149],\n",
       "       [ 96, 144, 124],\n",
       "       [106, 145,  57],\n",
       "       [ 87,  62,  24],\n",
       "       [ 17, 126,  53],\n",
       "       [ 55,   3, 103],\n",
       "       [ 92, 111,  61],\n",
       "       [ 68, 122,  37],\n",
       "       [145,  12,  27],\n",
       "       [ 47, 139,  38],\n",
       "       [113,   1, 109],\n",
       "       [132, 137, 148],\n",
       "       [128,  81,  87],\n",
       "       [ 75,  41, 117],\n",
       "       [ 92, 139, 136],\n",
       "       [143,  50,  79],\n",
       "       [ 63,  47,  91],\n",
       "       [ 27, 131, 143],\n",
       "       [ 35, 107, 135],\n",
       "       [  1, 108, 118],\n",
       "       [ 70, 108, 120],\n",
       "       [ 59,  11,   5],\n",
       "       [102, 141,  94],\n",
       "       [130,  94, 143],\n",
       "       [141,  99,  28],\n",
       "       [ 79, 113, 138],\n",
       "       [ 35,  68, 111],\n",
       "       [122,  92,   3],\n",
       "       [ 53,  49, 146],\n",
       "       [  4,  35, 100],\n",
       "       [ 68,   4,  89],\n",
       "       [ 92, 131, 132],\n",
       "       [ 89,  88,  90],\n",
       "       [114, 149,  11],\n",
       "       [124,  26, 110],\n",
       "       [144, 138,  92],\n",
       "       [150, 122,  97],\n",
       "       [  8,  64,  76],\n",
       "       [115, 118,  11],\n",
       "       [ 74, 108,  15],\n",
       "       [ 84, 100,   8],\n",
       "       [ 72,  85,  98],\n",
       "       [141,  78,  64],\n",
       "       [ 12,  19,  48],\n",
       "       [ 20,  80,  99],\n",
       "       [ 56,  70,   7],\n",
       "       [ 19,  82, 143],\n",
       "       [130,  19,  70],\n",
       "       [  7,  94, 148],\n",
       "       [105,   6,  68],\n",
       "       [ 34,  73,  93],\n",
       "       [ 95,  26,  24],\n",
       "       [ 54,  20, 136],\n",
       "       [101, 136,  46],\n",
       "       [ 81,  18,  32],\n",
       "       [127,  71,  43],\n",
       "       [  2,  41,  44],\n",
       "       [ 97, 110, 124],\n",
       "       [123,  70, 138],\n",
       "       [150,  70, 111],\n",
       "       [ 45,   5,  10]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g5 = np.random.randint(0,151,size = (100,3))\n",
    "g5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([144, 136, 144, 141, 129, 120, 142, 120, 138, 126, 135, 128, 129,\n",
       "       140, 128, 149, 148, 123, 126, 147, 130, 133, 147, 143, 131, 134,\n",
       "       131, 144, 149, 129, 149, 144, 124, 145, 126, 122, 145, 139, 132,\n",
       "       137, 148, 128, 139, 136, 143, 131, 143, 135, 120, 141, 130, 143,\n",
       "       141, 138, 122, 146, 131, 132, 149, 124, 144, 138, 150, 122, 141,\n",
       "       143, 130, 148, 136, 136, 127, 124, 123, 138, 150])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r = g5 >=120 # 布尔值\n",
    "r1= g5[r]\n",
    "r1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([17,  1,  9, 29, 28, 29, 14,  0, 18, 23, 21, 25, 18,  8, 23, 27, 21,\n",
       "       10, 24,  4, 28,  2, 25,  8, 24, 17,  3, 12, 27,  1, 27,  1, 11,  5,\n",
       "       28,  3,  4,  4, 11, 26,  8, 11, 15,  8, 12, 19, 20,  7, 19, 19,  7,\n",
       "        6, 26, 24, 20, 18,  2,  5, 10])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r2 = g5 <= 30\n",
    "r21 = g5[r2]\n",
    "r21"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[120, 142, 120],\n",
       "       [149, 129, 149],\n",
       "       [132, 137, 148]])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 筛选出三个数都大于120的数组\n",
    "cond2 = r[:,0]*r[:,1]*r[:,2]\n",
    "g5[cond2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "### 形状操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[40, 24, 38],\n",
       "       [35, 50, 41],\n",
       "       [29, 24, 45],\n",
       "       [51, 34, 29],\n",
       "       [40, 50, 33]])"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组变形\n",
    "h1 = np.random.randint(23,56,size = (3,5))\n",
    "h2 = np.random.randint(23,56,size = (3,5))\n",
    "h1.reshape(5,3,1) # 可以改成同等二维，或三维\n",
    "h1.reshape(-1,3) # 自动整形，等同于h1.reshape（5，3）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[36, 45, 23],\n",
       "       [28, 29, 48],\n",
       "       [24, 38, 47],\n",
       "       [32, 34, 46],\n",
       "       [44, 23, 28]])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组转置,行变列，列变行\n",
    "h1.T # T为transpose的缩写\n",
    "np.transpose(h1,(1,0)) # 二维默认为（0,1）\n",
    "np.transpose(h1,(2,1,0)) # 三维默认为（0,1,2）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[40, 24, 38, 35, 50],\n",
       "       [41, 29, 24, 45, 51],\n",
       "       [34, 29, 40, 50, 33],\n",
       "       [39, 33, 25, 46, 30],\n",
       "       [47, 53, 24, 26, 41],\n",
       "       [45, 35, 32, 43, 46],\n",
       "       [40, 24, 38, 35, 50],\n",
       "       [41, 29, 24, 45, 51],\n",
       "       [34, 29, 40, 50, 33]])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据堆叠合并\n",
    "np.concatenate([h1,h2]) # 默认合并后行数增加\n",
    "np.concatenate([h1,h2],axis = 1) # axis 代表轴/方向，默认为0\n",
    "np.hstack((h1,h2,h2)) # h代表水平，列增多，中间放元组\n",
    "np.vstack((h1,h2,h1)) # v代表竖直，行增多，中间放元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([], shape=(0, 25), dtype=int32),\n",
       " array([[151, 166, 183,   3,  34,  12,  44, 161, 119, 143, 163, 179, 118,\n",
       "         129, 163, 174, 102, 142,  68,  61,  96, 110, 196, 134, 124],\n",
       "        [133, 105, 170, 127, 132, 142, 184, 168,  29, 105,  56, 174,  43,\n",
       "         151,  80, 120,   4,  77, 139, 150, 166,  19, 181, 142, 136],\n",
       "        [146,  53,  46, 178, 102,  52,  45,  64, 108,  93,  50,   6, 146,\n",
       "         190,  67,  93, 120, 181, 144,  68, 118, 131,  46,  43,  63],\n",
       "        [ 21,  65,  73, 187,  74,  13, 136, 149,  87,  74,  74, 184,  80,\n",
       "          36, 196, 181,  42, 113, 178, 182,  99,  57,  97,  54,   0]]),\n",
       " array([[119,  95, 175, 186, 148, 143,   9, 188, 194,  93,  83, 184,  67,\n",
       "          54,  16, 172, 101, 162,  36,  25,  23,  26,  80,  79,  57],\n",
       "        [134,  72,   1,  46, 175, 109, 152, 106, 163,  51,  86, 135, 148,\n",
       "         148, 119, 198, 126,  32,  78,   5,  11, 102,  50,  10, 124],\n",
       "        [167,   5,  38, 188, 108, 189,  76, 182,   1, 179,  86, 183, 184,\n",
       "         116, 111,  73,  28, 168,  61, 133,  57,  80, 194, 195,   4]]),\n",
       " array([[ 74,  86, 163,  35, 116, 154, 192,  86, 119, 117,  74,  59, 156,\n",
       "          96, 197,  12,  85,  61,  92,  65, 189,  77, 167, 129, 194],\n",
       "        [ 80,  42, 125, 169,  61, 132, 174, 134, 133, 163,  54, 176, 128,\n",
       "         185,  41, 104,  62,  35, 137,  49,  96, 182, 176, 168, 137],\n",
       "        [161, 102, 161,  25, 119,  77, 187,  40,  69,  68, 190,  77, 117,\n",
       "          16,  26,  83, 112, 114,  80,  48, 105,  71,  30,  16, 166]])]"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组拆分\n",
    "h2 = np.random.randint(0,199,size = (10,25))\n",
    "np.split(h2,indices_or_sections = 5) # 平均分成5份\n",
    "np.split(h2,indices_or_sections = 5,axis = 1) # 指定拆分，按列切5份\n",
    "np.split(h2,indices_or_sections = [0,4,7]) # 根据索引拆分，0-4，4-7，7-尾\n",
    "np.hsplit(h2,indices_or_sections = 5) # 水平列的方向上拆分5份\n",
    "np.vsplit(h2,indices_or_sections = [0,4,7]) # 竖直行的方向上拆分"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "### 广播机制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "k1 = np.array([2,3,4]*4) # 把数组重复4遍\n",
    "k1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 2, 2],\n",
       "       [2, 3, 3],\n",
       "       [3, 3, 4],\n",
       "       [4, 4, 4]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "k1.sort() # 排序，默认从小到大排列\n",
    "k2 = k1.reshape(4,3)\n",
    "k2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[3, 7, 3],\n",
       "        [5, 4, 6],\n",
       "        [5, 4, 4],\n",
       "        [7, 7, 7]],\n",
       "\n",
       "       [[4, 2, 7],\n",
       "        [4, 6, 2],\n",
       "        [6, 5, 3],\n",
       "        [6, 4, 3]],\n",
       "\n",
       "       [[3, 6, 2],\n",
       "        [4, 6, 3],\n",
       "        [7, 5, 7],\n",
       "        [2, 3, 7]],\n",
       "\n",
       "       [[5, 7, 5],\n",
       "        [3, 5, 6],\n",
       "        [7, 3, 6],\n",
       "        [6, 4, 5]],\n",
       "\n",
       "       [[4, 2, 4],\n",
       "        [2, 4, 5],\n",
       "        [3, 5, 2],\n",
       "        [5, 5, 5]]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "k3 = np.random.randint(2,8,size = (5,4,3))\n",
    "k3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 6, 14,  6],\n",
       "        [10, 12, 18],\n",
       "        [15, 12, 16],\n",
       "        [28, 28, 28]],\n",
       "\n",
       "       [[ 8,  4, 14],\n",
       "        [ 8, 18,  6],\n",
       "        [18, 15, 12],\n",
       "        [24, 16, 12]],\n",
       "\n",
       "       [[ 6, 12,  4],\n",
       "        [ 8, 18,  9],\n",
       "        [21, 15, 28],\n",
       "        [ 8, 12, 28]],\n",
       "\n",
       "       [[10, 14, 10],\n",
       "        [ 6, 15, 18],\n",
       "        [21,  9, 24],\n",
       "        [24, 16, 20]],\n",
       "\n",
       "       [[ 8,  4,  8],\n",
       "        [ 4, 12, 15],\n",
       "        [ 9, 15,  8],\n",
       "        [20, 20, 20]]])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 和高维度的数组计算，要保持低维度一致，\n",
    "# 如上必须是（任意值，4，3）这样的格式\n",
    "k2 * k3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "### 通用函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# 元素级数字级别\n",
    "# abs sqrt square exp log sin cos tan maximum minimum\n",
    "# all any inner clip round trace ceil floor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.11045108, -1.45517944,  0.84905847, -0.42711816,  0.34141452,\n",
       "        -0.81992455, -1.86285482,  1.51760206],\n",
       "       [ 0.80757869,  0.60850136,  0.96386811, -0.52687381,  0.11913999,\n",
       "        -1.47560116,  1.4399151 ,  0.00523475],\n",
       "       [ 1.09001368,  0.3105924 ,  1.91089639, -0.70084357, -0.43525319,\n",
       "         0.44795466, -1.62390167,  1.34341038],\n",
       "       [-0.3802431 , -0.48361704,  1.33964427,  2.16897038, -1.85163369,\n",
       "         1.12629476, -1.67848114, -0.8475376 ],\n",
       "       [-0.07427779, -0.36404529,  0.42708889,  1.25341795, -1.29742367,\n",
       "        -0.4287419 ,  1.69682953, -0.94300457],\n",
       "       [-1.12345431,  1.05251731,  0.48007345, -0.45302638, -0.19787269,\n",
       "        -1.31172406, -0.5320546 ,  0.34680908],\n",
       "       [-1.06268049,  1.46577442,  0.82330887, -0.40977784,  0.51036945,\n",
       "        -1.22442989,  1.1989136 ,  1.00627516],\n",
       "       [ 1.3452584 , -0.28887406,  0.30554534, -0.64702617,  0.55177772,\n",
       "         0.93304918,  1.55586236, -1.03892012],\n",
       "       [ 0.31642071, -1.20610731,  0.48920389,  1.47321266,  0.88463493,\n",
       "        -0.96949624,  1.35055624,  0.92133983],\n",
       "       [ 1.83485151, -0.77447657,  1.43811198,  0.1417804 ,  2.03843249,\n",
       "         0.90527625,  1.09843592, -0.97040101],\n",
       "       [ 0.52815644, -1.37215233, -0.25327005,  0.41723989,  0.54747182,\n",
       "        -0.02384457,  0.31249776, -0.48363908],\n",
       "       [ 0.72036347, -0.42505062,  0.38291652,  1.34768219, -1.52469003,\n",
       "        -0.63111259, -1.1180733 ,  0.76963775],\n",
       "       [-2.36972604,  1.89808962,  0.1096133 , -0.36647382,  0.62091865,\n",
       "         0.4997842 ,  0.53270617, -0.90949614],\n",
       "       [ 0.70889166, -0.35575275, -0.19269859, -0.08978512, -0.01810879,\n",
       "         0.32206711, -0.00959675,  0.89540887],\n",
       "       [-1.02828823,  1.45418885,  0.73444312, -0.18692158, -0.30035126,\n",
       "        -0.78667394,  1.1602937 ,  0.2069312 ],\n",
       "       [-0.09531921,  1.10493206,  0.25881166,  0.72628114, -0.52891929,\n",
       "         0.52714489,  0.70932536,  1.1918391 ],\n",
       "       [-0.13719813, -0.49857618, -0.08292687, -0.47778193, -0.31931791,\n",
       "        -0.2868283 ,  0.2254785 ,  0.06179165],\n",
       "       [-0.46825411,  0.67395958,  0.95862959, -0.18710948,  0.64473516,\n",
       "         0.39841095,  0.36812429, -0.73302693],\n",
       "       [-0.4519675 , -0.68139462, -0.07241626, -0.7557921 ,  0.17688966,\n",
       "         0.81269808,  0.44695226, -1.81255872],\n",
       "       [-0.91801146, -1.26907872,  0.0743595 ,  0.77445146,  1.94095209,\n",
       "        -1.59116563,  1.08723673,  0.83501749]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[-3.54482894e-01, -4.40919800e-02,  1.18432789e-03,\n",
       "        -1.62516207e+00,  1.55920757e+00,  9.04033070e-02,\n",
       "         6.82702097e-01, -3.43101681e-01],\n",
       "       [-7.86586346e-01, -9.05354563e-01,  1.35654684e+00,\n",
       "        -1.12579528e+00,  2.18637443e-01, -1.32566951e+00,\n",
       "         6.10815186e-01,  1.51145689e+00],\n",
       "       [ 9.97929633e-01, -1.66223297e+00,  1.08018526e+00,\n",
       "        -6.39854870e-01, -6.69953375e-01,  3.05166292e-02,\n",
       "         1.28018937e-01, -4.55095927e-01],\n",
       "       [-1.65828438e+00, -8.07240860e-01, -6.40608551e-01,\n",
       "         5.49121976e-01, -1.73986670e-01, -5.29074393e-01,\n",
       "        -2.29213082e+00,  1.51612124e+00],\n",
       "       [-7.40889243e-01,  1.47179087e+00, -6.39923448e-01,\n",
       "         1.59241091e-01,  1.98562424e-01, -9.40539753e-01,\n",
       "        -3.47229647e-01, -1.97569945e-01],\n",
       "       [ 1.48299645e+00,  3.79346258e-01, -1.11633471e+00,\n",
       "         6.33955007e-01, -9.78046858e-01,  9.64605203e-01,\n",
       "        -1.35414614e+00,  2.59386419e+00],\n",
       "       [ 2.07534590e+00,  1.21275982e+00, -6.35249066e-01,\n",
       "         7.81864059e-01,  1.08849084e+00,  2.38942332e-01,\n",
       "         5.61430460e-01,  4.34153461e-01],\n",
       "       [ 2.41784358e+00,  1.27280565e+00, -7.65129933e-01,\n",
       "         6.34742733e-01, -8.72363536e-01,  6.69122391e-01,\n",
       "         4.38362075e-01,  1.00809379e+00],\n",
       "       [-7.56910225e-01, -1.61388432e-01,  9.27047889e-01,\n",
       "         1.71616873e-01,  8.61588561e-01, -1.65498855e+00,\n",
       "         1.08251007e+00,  1.08082152e+00],\n",
       "       [ 5.52295794e-01,  9.36512907e-01,  3.07620686e-01,\n",
       "        -3.95339094e-01, -8.36042613e-01,  7.70964793e-01,\n",
       "         3.48114987e-01, -3.72085465e-01],\n",
       "       [ 1.72737049e+00,  1.94101628e+00, -3.29276274e-01,\n",
       "        -8.24193665e-01, -9.44295650e-01,  8.87736466e-01,\n",
       "        -5.12438825e-01, -2.26403527e-01],\n",
       "       [-1.23685609e-01,  1.49977568e-01, -1.93828355e-01,\n",
       "         7.43899705e-01,  6.76057858e-01, -8.56279432e-02,\n",
       "        -1.19515734e+00, -4.23576102e-01],\n",
       "       [-1.24998994e+00, -3.99044618e-01,  7.10295777e-01,\n",
       "        -1.08308511e+00, -1.35853213e+00, -1.44726869e+00,\n",
       "         7.49907592e-01,  1.05789892e+00],\n",
       "       [ 5.51025139e-01,  6.57751033e-01, -1.46366309e-01,\n",
       "        -1.99941856e+00,  3.37069883e-01, -4.06740334e-01,\n",
       "        -5.99010668e-01, -1.25431415e-01],\n",
       "       [-9.87701735e-01,  8.17815182e-01,  1.10591244e+00,\n",
       "         2.58451865e-01,  1.77589241e+00, -7.67747632e-01,\n",
       "        -1.39910478e+00, -1.00200775e+00],\n",
       "       [-1.50692816e-01, -1.31375276e-01, -1.44674473e+00,\n",
       "         1.98546355e-01, -1.02252549e+00, -5.66670147e-01,\n",
       "         1.01043386e+00, -2.53172890e-01],\n",
       "       [-4.68032749e-01, -2.25972776e-01,  1.59707690e+00,\n",
       "         3.77549306e-01,  4.59462822e-01, -1.37641930e+00,\n",
       "         4.02235691e-01, -1.34244445e+00],\n",
       "       [-1.24069952e-01, -1.75089634e+00,  1.91947011e+00,\n",
       "        -2.88682005e-01,  1.38002756e+00,  2.24119492e-01,\n",
       "        -1.21594427e+00, -7.15673367e-01],\n",
       "       [ 6.04523699e-01,  4.10074570e-01, -1.71930985e+00,\n",
       "         1.54527993e-01,  5.23662151e-01, -4.38017375e-01,\n",
       "         1.37014683e+00,  5.70156763e-01],\n",
       "       [-1.89595323e+00, -9.02610702e-01,  2.58389606e-01,\n",
       "        -2.21242983e-01, -6.26051175e-01, -1.62399715e+00,\n",
       "        -3.34644014e-01,  1.51496621e+00]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "m1 = np.random.randn(20,8)\n",
    "m2 = np.random.randn(20,8)\n",
    "display(m1,m2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.27610707, 0.26926557, 0.82018846, 0.35127172, 0.75592308,\n",
       "        0.17271917, 1.52900138, 0.98385782],\n",
       "       [1.26163346, 0.29002628, 2.06743044, 0.04767796, 1.34344389,\n",
       "        1.47263349, 0.34656959, 0.063255  ],\n",
       "       [1.38874609, 1.26595267, 1.47031439, 0.1385585 , 0.03424031,\n",
       "        0.68297352, 0.20527988, 0.15863651],\n",
       "       [0.39998386, 0.35358063, 0.51321279, 0.45448578, 0.18911356,\n",
       "        0.06862962, 0.44175991, 0.15323482],\n",
       "       [0.39564297, 2.07478463, 0.88295542, 0.6785413 , 1.39043818,\n",
       "        0.45912192, 0.93091654, 1.54193849],\n",
       "       [0.65340887, 0.44271725, 0.0872554 , 0.51726443, 0.1996056 ,\n",
       "        1.63335876, 1.18326824, 1.94286141],\n",
       "       [2.62273535, 0.92498724, 0.80688362, 1.02239681, 1.16158137,\n",
       "        0.04003764, 0.8358468 , 0.30997637],\n",
       "       [0.07039686, 0.60076632, 0.36500563, 0.49752018, 0.95363905,\n",
       "        1.33898784, 0.87294401, 0.5783167 ],\n",
       "       [0.70549517, 0.72050011, 0.22218128, 0.82999929, 0.80097561,\n",
       "        1.31992086, 0.31407614, 0.01094205],\n",
       "       [0.92182839, 0.38041665, 0.41455727, 1.73352692, 0.22307949,\n",
       "        0.09336479, 2.25306551, 1.40903891],\n",
       "       [1.19717643, 0.4630144 , 0.51133678, 0.97578864, 1.95314042,\n",
       "        0.0722788 , 0.50343382, 0.50408247],\n",
       "       [1.49715798, 1.44279398, 0.46743348, 0.52467782, 0.1872927 ,\n",
       "        0.55480048, 0.18047266, 0.39429615],\n",
       "       [0.93789683, 0.0898642 , 1.53144194, 0.33042271, 0.38808303,\n",
       "        1.0202826 , 0.18688677, 1.60427259],\n",
       "       [0.20445742, 0.46244044, 0.12658298, 0.23051633, 0.47684129,\n",
       "        0.08194427, 1.01916091, 0.19636147],\n",
       "       [1.11291458, 0.08021068, 0.16401901, 0.46652476, 0.70016294,\n",
       "        0.38738668, 0.36552889, 0.93521455],\n",
       "       [0.27739249, 1.32700954, 0.79222817, 0.32074725, 0.88369593,\n",
       "        0.4702195 , 2.13458851, 0.62274741],\n",
       "       [1.55640743, 0.1967468 , 0.31941082, 0.28692941, 0.82203121,\n",
       "        0.84236422, 0.01626273, 1.15826894],\n",
       "       [0.00457994, 1.00072171, 0.96416664, 0.80388988, 1.47635349,\n",
       "        0.04754457, 0.07454115, 0.34508611],\n",
       "       [0.53278867, 0.39340937, 0.04399692, 0.06807573, 0.82403181,\n",
       "        0.72447946, 1.24519887, 0.28626115],\n",
       "       [0.25783086, 0.07607582, 1.38089613, 1.57781024, 0.25265214,\n",
       "        2.08066823, 1.44095288, 0.33307339]])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# m1.abs() # 对象方法调用绝对值，在python中适用\n",
    "np.abs(m1) # 模块方法调用绝对值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-82-f61e0a68d238>:1: RuntimeWarning: invalid value encountered in sqrt\n",
      "  np.sqrt(m1) # 开平方，不能有0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sqrt(m1) # 开平方，不能有0\n",
    "np.square(m1) # 平方\n",
    "np.exp(3) # 自然底数e的3次幂\n",
    "np.log(20) # 自然底数e的对数\n",
    "np.sin(np.pi/2) # pi的二分之一为90°\n",
    "np.cos(0) \n",
    "np.tan(np.pi/6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "operands could not be broadcast together with shapes (20,8) (2,8) ",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-71-2193ab71853b>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmaximum\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mm1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mm2\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 取最大值\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mminimum\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mm1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mm2\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 取最小值\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: operands could not be broadcast together with shapes (20,8) (2,8) "
     ]
    }
   ],
   "source": [
    "np.maximum(m1,m2) # 取最大值\n",
    "np.minimum(m1,m2) # 取最小值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m1.all() # 所有的为ture，则返回ture\n",
    "m1.any() # 只要有一个为ture，则返回ture"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.61928796, -1.93451294],\n",
       "       [ 3.26224655,  4.14243328],\n",
       "       [-1.21879159,  2.54724942],\n",
       "       [-0.11094847,  0.57443069],\n",
       "       [ 0.20938726, -2.77781491],\n",
       "       [-4.17767391, -1.69222679],\n",
       "       [ 0.56067908,  1.11652557],\n",
       "       [ 1.25806987, -1.98294668],\n",
       "       [-0.47267427,  0.30457482],\n",
       "       [-2.65116541, -0.89903254],\n",
       "       [ 1.20177131,  1.25035405],\n",
       "       [-0.66132509,  1.28467655],\n",
       "       [-3.04753972, -0.73662677],\n",
       "       [-0.63565252,  0.36608209],\n",
       "       [-1.23926002, -1.47510223],\n",
       "       [-1.86967666, -2.10817792],\n",
       "       [-0.37879914,  1.74370355],\n",
       "       [-2.83954198, -0.77505293],\n",
       "       [ 2.74113361,  4.37542986],\n",
       "       [-4.96867223,  0.22736225]])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.inner(m1,m2) # 数组对应位置相乘后加和，两个数组的内积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.11045108, -1.45517944,  0.        , -0.42711816,  0.        ,\n",
       "        -0.81992455, -1.86285482,  0.        ],\n",
       "       [ 0.        ,  0.        ,  0.        , -0.52687381,  0.        ,\n",
       "        -1.47560116,  0.        ,  0.        ],\n",
       "       [ 0.        ,  0.        ,  0.        , -0.70084357, -0.43525319,\n",
       "         0.        , -1.62390167,  0.        ],\n",
       "       [-0.3802431 , -0.48361704,  0.        ,  0.        , -1.85163369,\n",
       "         0.        , -1.67848114, -0.8475376 ],\n",
       "       [-0.07427779, -0.36404529,  0.        ,  0.        , -1.29742367,\n",
       "        -0.4287419 ,  0.        , -0.94300457],\n",
       "       [-1.12345431,  0.        ,  0.        , -0.45302638, -0.19787269,\n",
       "        -1.31172406, -0.5320546 ,  0.        ],\n",
       "       [-1.06268049,  0.        ,  0.        , -0.40977784,  0.        ,\n",
       "        -1.22442989,  0.        ,  0.        ],\n",
       "       [ 0.        , -0.28887406,  0.        , -0.64702617,  0.        ,\n",
       "         0.        ,  0.        , -1.03892012],\n",
       "       [ 0.        , -1.20610731,  0.        ,  0.        ,  0.        ,\n",
       "        -0.96949624,  0.        ,  0.        ],\n",
       "       [ 0.        , -0.77447657,  0.        ,  0.        ,  0.        ,\n",
       "         0.        ,  0.        , -0.97040101],\n",
       "       [ 0.        , -1.37215233, -0.25327005,  0.        ,  0.        ,\n",
       "        -0.02384457,  0.        , -0.48363908],\n",
       "       [ 0.        , -0.42505062,  0.        ,  0.        , -1.52469003,\n",
       "        -0.63111259, -1.1180733 ,  0.        ],\n",
       "       [-2.36972604,  0.        ,  0.        , -0.36647382,  0.        ,\n",
       "         0.        ,  0.        , -0.90949614],\n",
       "       [ 0.        , -0.35575275, -0.19269859, -0.08978512, -0.01810879,\n",
       "         0.        , -0.00959675,  0.        ],\n",
       "       [-1.02828823,  0.        ,  0.        , -0.18692158, -0.30035126,\n",
       "        -0.78667394,  0.        ,  0.        ],\n",
       "       [-0.09531921,  0.        ,  0.        ,  0.        , -0.52891929,\n",
       "         0.        ,  0.        ,  0.        ],\n",
       "       [-0.13719813, -0.49857618, -0.08292687, -0.47778193, -0.31931791,\n",
       "        -0.2868283 ,  0.        ,  0.        ],\n",
       "       [-0.46825411,  0.        ,  0.        , -0.18710948,  0.        ,\n",
       "         0.        ,  0.        , -0.73302693],\n",
       "       [-0.4519675 , -0.68139462, -0.07241626, -0.7557921 ,  0.        ,\n",
       "         0.        ,  0.        , -1.81255872],\n",
       "       [-0.91801146, -1.26907872,  0.        ,  0.        ,  0.        ,\n",
       "        -1.59116563,  0.        ,  0.        ]])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.clip(m1,-5,0) # 数据裁剪，小于-5的变成-5，大于0的变为0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1., -2.,  0., -1.,  0., -1., -2.,  1.],\n",
       "       [ 0.,  0.,  0., -1.,  0., -2.,  1.,  0.],\n",
       "       [ 1.,  0.,  1., -1., -1.,  0., -2.,  1.],\n",
       "       [-1., -1.,  1.,  2., -2.,  1., -2., -1.],\n",
       "       [-1., -1.,  0.,  1., -2., -1.,  1., -1.],\n",
       "       [-2.,  1.,  0., -1., -1., -2., -1.,  0.],\n",
       "       [-2.,  1.,  0., -1.,  0., -2.,  1.,  1.],\n",
       "       [ 1., -1.,  0., -1.,  0.,  0.,  1., -2.],\n",
       "       [ 0., -2.,  0.,  1.,  0., -1.,  1.,  0.],\n",
       "       [ 1., -1.,  1.,  0.,  2.,  0.,  1., -1.],\n",
       "       [ 0., -2., -1.,  0.,  0., -1.,  0., -1.],\n",
       "       [ 0., -1.,  0.,  1., -2., -1., -2.,  0.],\n",
       "       [-3.,  1.,  0., -1.,  0.,  0.,  0., -1.],\n",
       "       [ 0., -1., -1., -1., -1.,  0., -1.,  0.],\n",
       "       [-2.,  1.,  0., -1., -1., -1.,  1.,  0.],\n",
       "       [-1.,  1.,  0.,  0., -1.,  0.,  0.,  1.],\n",
       "       [-1., -1., -1., -1., -1., -1.,  0.,  0.],\n",
       "       [-1.,  0.,  0., -1.,  0.,  0.,  0., -1.],\n",
       "       [-1., -1., -1., -1.,  0.,  0.,  0., -2.],\n",
       "       [-1., -2.,  0.,  0.,  1., -2.,  1.,  0.]])"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m1.round(2) # 保留2位小数\n",
    "np.ceil(m1) # 向上取整\n",
    "np.floor(m1) # 向下取整"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.128762794230028"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.trace(m1) # 计算对角线上的和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### where函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[82, 35],\n",
       "       [86, 34],\n",
       "       [81, 69],\n",
       "       [66, 80]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([67, 45, 75, 58, 86, 25, 19, 30, 57, 69, 79, 34, 65, 53, 37, 27, 82,\n",
       "       15])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "n1 = np.random.randint(15,88,size = (4,2))\n",
    "n2 = np.random.randint(15,88,size = 18)\n",
    "display(n1,n2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  0,  0,  0, 91,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 87,\n",
       "        0])"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(n1>80,n1,0)\n",
    "np.where(n2<80,0,n2+5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([87, 65, 95, 78, 86, 25, 19, 30, 77, 89, 99, 34, 85, 73, 37, 27, 82,\n",
       "       15])"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n3 = (n2 >=40) & (n2 <= 80)\n",
    "np.where(n3,n2+20,n2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[35, 82],\n",
       "        [34, 86]],\n",
       "\n",
       "       [[35, 82],\n",
       "        [34, 86]],\n",
       "\n",
       "       [[35, 82],\n",
       "        [34, 86]],\n",
       "\n",
       "       [[35, 82],\n",
       "        [34, 86]]])"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sort(n1) # 调用模块方法排序，产生了新数组\n",
    "n1.sort() # 调用对象方法排序，是在原数组上进行\n",
    "n4 = n1.argsort() # 返回排序结果的索引\n",
    "n1[n4][::-1] # 由大到小"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'np' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-2-69d4be370be0>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mintersect1d\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mn1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mn2\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 交集intersect1d\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0munionld\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mn1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mn2\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 并集\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msetdiffldi\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mn1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mn2\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 差集\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0munique\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mn1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mn2\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 去重\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'np' is not defined"
     ]
    }
   ],
   "source": [
    "np.intersect1d(n1,n2) # 交集intersect1d\n",
    "np.unionld(n1,n2) # 并集\n",
    "np.setdiffldi(n1,n2) # 差集\n",
    "np.unique(n1,n2) # 去重"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "### 数学和统计函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[34, 29, 37, 36, 35],\n",
       "       [40, 35, 36, 41, 25],\n",
       "       [43, 24, 40, 42, 28]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[29, 28, 27, 40, 37],\n",
       "       [24, 43, 25, 38, 43],\n",
       "       [27, 40, 43, 36, 41]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "p1 = np.random.randint(23,45,size = (3,5))\n",
    "p2 = np.random.randint(23,45,size = (3,5))\n",
    "display(p1,p2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0],\n",
       "       [0, 1],\n",
       "       [0, 2],\n",
       "       [0, 3],\n",
       "       [0, 4],\n",
       "       [1, 0],\n",
       "       [1, 1],\n",
       "       [1, 2],\n",
       "       [1, 3],\n",
       "       [1, 4],\n",
       "       [2, 0],\n",
       "       [2, 1],\n",
       "       [2, 2],\n",
       "       [2, 3],\n",
       "       [2, 4]], dtype=int64)"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p1.min() # 取最小值\n",
    "p1.min(axis = 1) # 在列的方向上取最小值\n",
    "p2.max(axis = 0) # 在行的方向上取最大值\n",
    "p1.mean() # 平均值\n",
    "np.median(p2) # 中位数\n",
    "p1.sum() # 求和\n",
    "p2.std() # 标准差\n",
    "p1.var() # 方差，数据内部波动\n",
    "p1.cumsum() # 累计和\n",
    "p1.cumprod() # 累乘和\n",
    "p1.argmin() # 取最小值索引\n",
    "p1.argmax() # 取最大值索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0],\n",
       "       [0, 1],\n",
       "       [0, 2],\n",
       "       [0, 3],\n",
       "       [0, 4],\n",
       "       [1, 0],\n",
       "       [1, 1],\n",
       "       [1, 2],\n",
       "       [1, 3],\n",
       "       [1, 4],\n",
       "       [2, 0],\n",
       "       [2, 1],\n",
       "       [2, 2],\n",
       "       [2, 3],\n",
       "       [2, 4]], dtype=int64)"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p3 = np.argwhere((p1>30)|(p1<40)) # 取符合条件的索引\n",
    "p3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "34\n",
      "29\n",
      "37\n",
      "36\n",
      "35\n",
      "40\n",
      "35\n",
      "36\n",
      "41\n",
      "25\n",
      "43\n",
      "24\n",
      "40\n",
      "42\n",
      "28\n"
     ]
    }
   ],
   "source": [
    "# 用遍历取符合条件的索引对应的值,二维以上要用多个值\n",
    "for i,j in p3:\n",
    "    print(p1[i,j])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.        , 0.05816469, 0.70162055],\n",
       "       [0.05816469, 1.        , 0.70192047],\n",
       "       [0.70162055, 0.70192047, 1.        ]])"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cov(p1) # 协方差(属性内部计算)，与方差概念类似\n",
    "np.corrcoef(p1) # 相关性系数，与协方差有关，范围在-1至1间\n",
    "                # 1代表正相关，-1代表负相关，0代表无关"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "### 线性代数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 206,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 3,  7,  4, 10],\n",
       "        [ 7, 12, 11,  7],\n",
       "        [ 6,  8, 12, 11]],\n",
       "\n",
       "       [[11,  9,  2,  9],\n",
       "        [10,  2, 10, 14],\n",
       "        [13, 12,  5,  8]]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([14, 13,  9])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 矩阵的行和列的长度要相等，要符合矩阵交换律\n",
    "A = np.random.randint(2,15,size = (2,3,4))\n",
    "B = np.random.randint(2,15,size = 3)\n",
    "display(A,B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 192,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[180, 132, 183, 176],\n",
       "       [109, 223, 239, 100]])"
      ]
     },
     "execution_count": 192,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B.dot(A) # 矩阵乘法,点乘\n",
    "np.dot(B,A)\n",
    "B @ A "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 198,
   "metadata": {
    "collapsed": true,
    "hidden": true
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "shapes (2,3,4) and (3,) not aligned: 4 (dim 2) != 3 (dim 0)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-198-a4b4ef047e8a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mA\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mB\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mA\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mB\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mA\u001b[0m \u001b[1;33m@\u001b[0m \u001b[0mB\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: shapes (2,3,4) and (3,) not aligned: 4 (dim 2) != 3 (dim 0)"
     ]
    }
   ],
   "source": [
    "# 这样是报错的\n",
    "A.dot(B) \n",
    "np.dot(A,B)\n",
    "A @ B "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 鸢尾花"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 212,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5.1, 4.9, 4.7, 4.6, 5. , 5.4, 4.6, 5. , 4.4, 4.9, 5.4, 4.8, 4.8,\n",
       "       4.3, 5.8, 5.7, 5.4, 5.1, 5.7, 5.1, 5.4, 5.1, 4.6, 5.1, 4.8, 5. ,\n",
       "       5. , 5.2, 5.2, 4.7, 4.8, 5.4, 5.2, 5.5, 4.9, 5. , 5.5, 4.9, 4.4,\n",
       "       5.1, 5. , 4.5, 4.4, 5. , 5.1, 4.8, 5.1, 4.6, 5.3, 5. , 7. , 6.4,\n",
       "       6.9, 5.5, 6.5, 5.7, 6.3, 4.9, 6.6, 5.2, 5. , 5.9, 6. , 6.1, 5.6,\n",
       "       6.7, 5.6, 5.8, 6.2, 5.6, 5.9, 6.1, 6.3, 6.1, 6.4, 6.6, 6.8, 6.7,\n",
       "       6. , 5.7, 5.5, 5.5, 5.8, 6. , 5.4, 6. , 6.7, 6.3, 5.6, 5.5, 5.5,\n",
       "       6.1, 5.8, 5. , 5.6, 5.7, 5.7, 6.2, 5.1, 5.7, 6.3, 5.8, 7.1, 6.3,\n",
       "       6.5, 7.6, 4.9, 7.3, 6.7, 7.2, 6.5, 6.4, 6.8, 5.7, 5.8, 6.4, 6.5,\n",
       "       7.7, 7.7, 6. , 6.9, 5.6, 7.7, 6.3, 6.7, 7.2, 6.2, 6.1, 6.4, 7.2,\n",
       "       7.4, 7.9, 6.4, 6.3, 6.1, 7.7, 6.3, 6.4, 6. , 6.9, 6.7, 6.9, 5.8,\n",
       "       6.8, 6.7, 6.7, 6.3, 6.5, 6.2, 5.9])"
      ]
     },
     "execution_count": 212,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取本地数据\n",
    "C = np.loadtxt('F:\\【3】数据分析\\【5】python\\模块三\\iris.csv',delimiter = \",\")\n",
    "C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 214,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 存在jupyter内\n",
    "np.savetxt(fname = './IRIS.csv',\n",
    "          X = C,\n",
    "          fmt = '%0.2f',\n",
    "          delimiter = ',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 226,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.9"
      ]
     },
     "execution_count": 226,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C = np.sort(C) # 排序\n",
    "C = np.unique(C)\n",
    "C.sum()\n",
    "C.cumsum()\n",
    "C.mean()\n",
    "C.std()\n",
    "C.var()\n",
    "C.min()\n",
    "C.max()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
